package com.bupt.uchouten.common.register;

import com.bupt.uchouten.common.block.TenkaiVigorousVineBlock;
import com.bupt.uchouten.common.util.Reference;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.MapColor;
import net.minecraft.world.level.material.PushReaction;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;

import java.util.function.Function;
import java.util.function.Supplier;

public final class UchoutenBlocks {

    public static final DeferredRegister<Block> REGISTER = DeferredRegister.create(ForgeRegistries.BLOCKS, Reference.MODID);

    public static final class FunctionalBlocks {
        public static final BlockEntry<TenkaiVigorousVineBlock> TENKAI_VIGOROUS_VINE_BLOCK = new BlockEntry<>(
                "tenkai_vigorous_vine", () -> BlockBehaviour.Properties.of().mapColor(MapColor.PLANT)
                .strength(-1.0F, 3600000.0F)
                .sound(SoundType.VINE)
                .noCollission().noLootTable()
                .isValidSpawn((blockState, level, blockPos, entityType) -> false)
                .pushReaction(PushReaction.IGNORE), TenkaiVigorousVineBlock::new
        );

        private FunctionalBlocks() {
        }

        private static void init() {
            UchoutenItems.ItemEntry.register(TENKAI_VIGOROUS_VINE_BLOCK.getId().getPath(), () -> new BlockItem(TENKAI_VIGOROUS_VINE_BLOCK.get(), new Item.Properties()));
        }
    }

    private UchoutenBlocks() {
    }

    public static void init(IEventBus bus) {
        REGISTER.register(bus);

        FunctionalBlocks.init();
    }

    public static final class BlockEntry<T extends Block> implements Supplier<T>, ItemLike {
        private final RegistryObject<T> regObject;
        private final Supplier<BlockBehaviour.Properties> properties;

        public BlockEntry(String name, Supplier<BlockBehaviour.Properties> properties, Function<BlockBehaviour.Properties, T> make) {
            this.properties = properties;
            this.regObject = REGISTER.register(name, () -> make.apply(properties.get()));
        }

        @Override
        public T get() {
            return this.regObject.get();
        }

        public BlockState defaultBlockState() {
            return this.get().defaultBlockState();
        }

        public ResourceLocation getId() {
            return this.regObject.getId();
        }

        public BlockBehaviour.Properties getProperties() {
            return this.properties.get();
        }

        @Override
        public Item asItem() {
            return this.get().asItem();
        }
    }

}
